#ifndef _PV_H
#define _PV_H

/*
 * p: the p operation for applying resource.
 * @para semid: the semaphore's id.
 * @para index: the semaphore's index.
 * @return: if error happens return -1, return 0 is right.
 */
static int p(int semid, int index)
{
#if 1
	struct sembuf buf = {
		.sem_num = 0,			/*semaphore number*/
		.sem_op = -1,			/*semaphore operation*/
		.sem_flg = SEM_UNDO		/*operation flags*/
	};
#endif
	if (0 > index) {
		g_message("%s -- index of the semaphore is invalid!\n", __func__);
		return -1;
	}
	buf.sem_num = index;
	if (-1 == semop(semid, &buf, 1)) {
		g_message("%s -- process %d p operation failed!\n", __func__, getpid());
		return -1;
	}
	return 0;
}

/*
 * v: the v operation for releasing resource.
 * @para semid: the semaphore's id.
 * @para index: the semaphore's index.
 * @return: if error happens return -1, return 0 is right.
 */
static int v(int semid, int index)
{
#if 1
	struct sembuf buf = {
		.sem_num = 0,
		/* release resource, add the buf.sem_op to semval */
		.sem_op = 1,
		.sem_flg = SEM_UNDO
	};
#endif
	if (0 > index) {
		g_message("%s -- index of the semaphore is invalid!\n", __func__);
		return -1;
	}
	buf.sem_num = index;
	if (-1 == semop(semid, &buf, 1)) {
		g_message("%s -- process %d v operation failed!\n", __func__, getpid());
		return -1;
	}
	return 0;
}

#endif
